package com.yhyvr.console.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lz.kit.LogKit;
import com.lz.mybatis.jdbc.auto.dao.SysNavMapper;
import com.lz.mybatis.jdbc.auto.dao.SysNavRoleMapper;
import com.lz.mybatis.jdbc.auto.model.SysNav;
import com.lz.mybatis.jdbc.auto.model.SysNavExample;
import com.lz.mybatis.jdbc.auto.model.SysNavRole;
import com.lz.mybatis.jdbc.auto.model.SysNavRoleExample;
import com.lz.tool.LzStringUtils;
import com.lz.tool.UUIDTool;
import com.yhyvr.console.controller.ControllerQueryTool;
import com.yhyvr.pojo.req.console.BootStrapGridReq;
import com.yhyvr.pojo.resp.CommonResp;
import com.yhyvr.pojo.resp.HttpResult;
import com.yhyvr.pojo.resp.console.BootStrapGridResp;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * title:
 * </p>
 * <p>
 * description:
 * </p>
 *
 * @create 张钦
 * @update
 * @version 1.0
 * @date
 */
@Service
public class SysNavService{
	@Resource
	private SysNavMapper sysNavMapper;
	@Resource
	private SysNavRoleMapper sysNavRoleMapper;
	public String add(int p_id,Short level,ModelMap model){
		SysNav nav=new SysNav();
		nav.setPid(p_id);
		nav.setLevels(level);
		model.put("sysNav", nav);
		return "console/sysNav/sysNav_form";
	}

	public String edit(ModelMap modelMap,int id){
		modelMap.put("sysNav", sysNavMapper.selectByPrimaryKey(id));
		return "console/sysNav/sysNav_form";
	}

	public String list(String p_id,Short level,ModelMap modelMap)
	{
		modelMap.put("p_id",p_id);
		modelMap.put("level",level);
		return "console/sysNav/sysNav_table";
	}

	public BootStrapGridResp json(BootStrapGridReq bootGridReq){
		Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
		if(bootGridReq.getSort()!=null) {
			page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
		}
		SysNavExample sysNavExample = new SysNavExample();
		ControllerQueryTool.setSysNavCriteria(bootGridReq.getQuery(),sysNavExample.createCriteria());
		List<SysNav> sysNavList = sysNavMapper.selectByExample(sysNavExample);
		return new BootStrapGridResp(page.getTotal(),sysNavList);
	}

	public HttpResult save(String formObjectJson){
		try{
			SysNav sysNav= JSON.parseObject(formObjectJson,SysNav.class);
			Integer id=sysNav.getNavId();
			if (id!=null) {
				sysNavMapper.updateByPrimaryKeySelective(sysNav);
				return CommonResp.getSuccess();
			}else{
				sysNav.setUuid(UUIDTool.getUUID());
				sysNavMapper.insertSelective(sysNav);
				return CommonResp.getSuccess();
			}
		}catch(Exception e){
			LogKit.error(e.getMessage(),e);
			return CommonResp.getError();
		}

	}

	public
	HttpResult delete(String ids){
		String[]idsa=ids.split(",");
		for (String id : idsa) {
			SysNavRoleExample sysNavRoleExample = new SysNavRoleExample();
			sysNavRoleExample.createCriteria().andNavIdEqualTo(Integer.parseInt(id));
			sysNavRoleMapper.deleteByExample(sysNavRoleExample);
			deleteAllRoleNavByPid(Integer.parseInt(id));
			SysNavExample sysNavExample = new SysNavExample();
			sysNavExample.createCriteria().andPidEqualTo(Integer.parseInt(id));

			sysNavMapper.deleteByExample(sysNavExample);
			sysNavMapper.deleteByPrimaryKey(Integer.parseInt(id));

		}
		return CommonResp.getSuccessByMessage("操作成功!");
	}

	public void deleteAllRoleNavByPid(int id){
		SysNavExample sysNavExample = new SysNavExample();
		sysNavExample.createCriteria().andPidEqualTo(id);
		List<SysNav> list = sysNavMapper.selectByExample(sysNavExample);
		for (SysNav sysNav : list) {
			SysNavRoleExample sysNavRoleExample = new SysNavRoleExample();
			sysNavRoleExample.createCriteria().andNavIdEqualTo(sysNav.getNavId());
			sysNavRoleMapper.deleteByExample(sysNavRoleExample);
			deleteAllRoleNavByPid(sysNav.getNavId());
		}
	}
}
